package br.com.iswe.core.jasperreports;

import java.util.List;

import net.sf.jasperreports.engine.JRValueParameter;
import net.sf.jasperreports.engine.query.JRClauseFunction;
import net.sf.jasperreports.engine.query.JRClauseTokens;
import net.sf.jasperreports.engine.query.JRQueryClauseContext;

@SuppressWarnings("unchecked")
public class ListParameterClauseFunction implements JRClauseFunction{
	private String parameterName;
	private String expression;
	public ListParameterClauseFunction(String parameterName, String expression){
		this.parameterName = parameterName;
		this.expression = expression;
	}
	public void apply(JRClauseTokens clauseTokens, JRQueryClauseContext queryContext) {
		JRValueParameter parameter = queryContext.getValueParameter(parameterName);
		//Aplica o filtro para a  Lista
		List lista = (List)parameter.getValue();
		if(lista != null && lista.size() > 0){
			StringBuffer strBuffer = queryContext.queryBuffer();
			strBuffer.append(" and ");
			strBuffer.append(expression);
			strBuffer.append(" in (");
			for (int i = 0; i < lista.size(); i++) {
				strBuffer.append(lista.get(i));
				if((i+1) < lista.size()){
					strBuffer.append(", ");
				}
			}
			strBuffer.append(") \n");
		}
	}		
}
